package com.nsl.my_youlai.Jackson;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import enums.learn.ColorEnum;
import enums.learn.UseColorEnumBo;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Demo1 {
    @Test
    void test5() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Country country = new Country("元朝", new Leader("忽必烈", 79, "蒙古族"));
        System.out.println(objectMapper.writeValueAsString(country));
    }

    @Test
    void test6() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Leader leader = new Leader("忽必烈", 79, "蒙古族");
        Country country = new Country("元朝", leader);
        System.out.println(objectMapper.writeValueAsString(leader));
        System.out.println(objectMapper.writeValueAsString(country));
    }




    @Test
    void test8() throws JsonProcessingException {
        UseColorEnumBo useColorEnumBo=new UseColorEnumBo();
        useColorEnumBo.setColorEnum(ColorEnum.YELLOW);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString=objectMapper.writeValueAsString(useColorEnumBo);
        String jsonS="{\"colorEnum\":{\"value\":1,\"label\":\"黄\",\"enumCnName\":\"颜色类型\",\"labelRangeAsString\":\"[红, 黄]\"},\"value\":null}";
        System.out.println(objectMapper.writeValueAsString(useColorEnumBo));

        ObjectMapper objectMapper2 = new ObjectMapper();
        //SimpleModule module = new SimpleModule();
        //module.addDeserializer(ColorEnum.class, new ColorEnumDeserializer());
        //objectMapper2.registerModules(module);

        System.out.println(objectMapper2.readValue(jsonS, UseColorEnumBo.class));
    }


    //ok
    @Test
    void test9() throws IOException {
        String jsonS="{\"colorEnum\":\"{\\\"label\\\":\\\"黄\\\",\\\"value\\\":1}\",\"value\":null}";
        JsonFactory factory = new JsonFactory();
        JsonParser  parser  = factory.createParser(jsonS);
        while(!parser.isClosed()){
            JsonToken jsonToken = parser.nextToken();

            if(JsonToken.FIELD_NAME.equals(jsonToken)){
                String fieldName = parser.getCurrentName();
                //System.out.println(fieldName);
                jsonToken = parser.nextToken();
                String stringValue=parser.getValueAsString();
                System.out.println(fieldName+" = "+stringValue);
                System.out.println("*******************");

            }
        }
    }

    @Test
    void test10() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String jsonS="{\"colorEnum\":\"{\\\"label\\\":\\\"黄\\\",\\\"value\\\":1}\",\"value\":null}";
        JsonFactory factory = new JsonFactory(mapper);
        JsonParser  parser  = factory.createParser(jsonS);
        TreeNode treeNode=parser.getCodec().readTree(parser);
        System.out.println(treeNode.isObject());
        //get(index)应该是数组的用法
       // System.out.println(treeNode.get(0));
        while(!parser.isClosed()){
            JsonToken jsonToken = parser.nextToken();

            if(JsonToken.FIELD_NAME.equals(jsonToken)){
                String fieldName = parser.getCurrentName();
                System.out.println(fieldName);

                jsonToken = parser.nextToken();
                System.out.println(parser.getValueAsString());
                System.out.println("*******************");

            }
        }
    }
}
